﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Diagnostics;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using Microsoft.Phone;
using Microsoft.Phone.Scheduler;
using System.Windows.Navigation;

namespace Flag
{

    public partial class menu : PhoneApplicationPage
    {
        Player player;
        string user;
        GeoCoordinateWatcher watcher;  // main location object

        public menu()
        {
            InitializeComponent();
            player = new Player();

            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy;
            watcher.MovementThreshold = 25.0f; // meters of change before "PositionChanged"

            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_statusChanged);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

            watcher.TryStart(true, TimeSpan.FromMilliseconds(60000));

            // A unique name for your task. It is used to
            // locate it in from the service.
            var taskName = "MyTask";

            // If the task exists
            var oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
            if (oldTask != null)
            {
                ScheduledActionService.Remove(taskName);
            }

            // Create the Task
            PeriodicTask task = new PeriodicTask(taskName);

            // Description is required
            task.Description = "Notification";

            // Add it to the service to execute
            ScheduledActionService.Add(task);

        }
       
        void watcher_statusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    // The Location Service is disabled or unsupported.
                    // Check to see if the user has disabled the location service.
                    if (watcher.Permission == GeoPositionPermission.Denied)
                    {
                        // the user has disabled LocServ on their device.
                        textStatus.Text = "You have disabled Location Service.";
                    }
                    else
                    {
                        textStatus.Text = "Location Service is not\nfunctioning on this device.";
                    }
                    break;

                case GeoPositionStatus.Initializing:
                    // The location service is initializing.
                    textStatus.Text = "Location Service is retrieving data...";
                    break;

                case GeoPositionStatus.NoData:
                    // The Location Service is working, but it cannot get location data
                    // due to poor signal fidelity (most likely)
                    textStatus.Text = "Location data is not available.";
                    break;

                case GeoPositionStatus.Ready:
                    // The location service is working and is receiving location data.
                    textStatus.Text = "Location data is available.";
                    break;
            }
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {

            // update the current position
            player.position = e.Position.Location;
        }

        private void buttonCreate_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Waiting.xaml?User=" + user, UriKind.RelativeOrAbsolute));
            //TODO invia al server la posizione
        }

        private void buttonJoin_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Waiting.xaml?User=" + user, UriKind.RelativeOrAbsolute));
            //TODO invia al server la posizione
        }

        private void buttonMap_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/map.xaml?User="+user, UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Let base handle
            base.OnNavigatedTo(e);

            // Get the User
            user = NavigationContext.QueryString["User"];
        }

    }
}